package com.sbox;

import com.sbox.db.ContentMgr;
import com.sbox.model.NoteItem;
import com.sbox.model.SBoxConst;
import com.sbox.util.SBoxLog;
import com.sbox.util.Utils;
import com.sbox.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NoteActivity extends Activity {
	private NoteItem mNote = null;
	private TitleBar mTitleBar = null;
	private TextView mTextInfo = null;
	private LinedEditText mEditor = null;
	private Button mBtnDelete = null;
	private Button mBtnNext = null;
	private Button mBtnPrev = null;
	
	private View.OnClickListener mBackListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			String title = null;
			String content = null;
			
			content = mEditor.getText().toString();
			title = Utils.getTitle(content);
			
			if( mEditor.isTextChanged() && title != null && title.length() > 0 )
			{
				SBoxLog.info("text changted, save the change.");
				saveNote(title, content);
			}			
			
			//close input method:
			Utils.closeInputMethod(NoteActivity.this, v);			
			
			NoteActivity.this.finish();
		}
	};
	
	private View.OnClickListener mAddNoteListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			Intent i = new Intent(NoteActivity.this, NoteActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
			startActivityForResult(i, SBoxConst.REQUST_NOTE);
		}
	};
	
	private View.OnClickListener mDeleteListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {			
			Dialog dialog = new AlertDialog.Builder(NoteActivity.this).setMessage(R.string.str_confirm_delete).setPositiveButton(R.string.str_yes, mDeleteConfirmListener).setNegativeButton(R.string.str_cancel, mDeleteConfirmListener).create();				
			dialog.show();
		}
	};
	
	private DialogInterface.OnClickListener mDeleteConfirmListener = new DialogInterface.OnClickListener() 
	{		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			if( which == DialogInterface.BUTTON_POSITIVE )
			{
				if( mNote != null )
				{
					ContentMgr.getInstance(NoteActivity.this).deleteNote(mNote);
				}				
				NoteActivity.this.finish();
			}
			else
			{
			}
		}
	};	
	
	private View.OnClickListener mNextPrevListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {	
			String title = null;
			String content = null;
			
			content = mEditor.getText().toString();
			title = Utils.getTitle(content);
			
			if( mEditor.isTextChanged() && title != null && title.length() > 0 )
			{
				SBoxLog.info("text changted, save the change.");
				saveNote(title, content);
			}			
			
			//close input method:
			Utils.closeInputMethod(NoteActivity.this, v);			
			
			if( v == mBtnNext )
			{
				SBoxLog.info("Next");
				NoteActivity.this.setResult(SBoxConst.REQUEST_NOTE_NEXT);
			}
			else if( v == mBtnPrev )
			{
				SBoxLog.info("Prev");
				NoteActivity.this.setResult(SBoxConst.REQUEST_NOTE_PREV);
			}
			
			NoteActivity.this.finish();
		}
	};
	
	@Override
	public void onCreate(Bundle savedBuddle)
	{
		super.onCreate(savedBuddle);
		setContentView(R.layout.note_view);
		
		mTextInfo = (TextView)findViewById(R.id.note_info);		
		
		mEditor = (LinedEditText)findViewById(R.id.note_editor);		
		
		mTitleBar = (TitleBar)findViewById(R.id.note_title);
		mTitleBar.getButton1().setOnClickListener(mBackListener);		
		mTitleBar.getButton2().setImageResource(R.drawable.btn_add_note);
		mTitleBar.getButton2().setOnClickListener(mAddNoteListener);

		mBtnDelete = (Button)findViewById(R.id.btn_note_delete);
		mBtnDelete.setOnClickListener(mDeleteListener);
		
		mBtnNext = (Button)findViewById(R.id.btn_note_next);
		mBtnNext.setOnClickListener(mNextPrevListener);
		
		mBtnPrev = (Button)findViewById(R.id.btn_note_prev);
		mBtnPrev.setOnClickListener(mNextPrevListener);
		
		
		updateUI();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		mBackListener.onClick(mTitleBar.getButton1());
    }
	
	private void updateUI() {
		Intent i = getIntent();
		Bundle bundle = i.getExtras();
		if( bundle != null )
		{
			long id = i.getExtras().getLong(SBoxConst.KEY_NOTE_ID);
			if( id > 0 )
			{
				mNote =ContentMgr.getInstance(this).loadNote(id);				
				if( mNote != null )
				{
					mTextInfo.setText( Utils.formatTime(mNote.ID) );
					mEditor.setText(mNote.Content);
					mTitleBar.getText().setText(mNote.Title);
				}
			}
		}
	}
	
	private void saveNote(String title, String content)
	{
		if( mNote == null )
		{
			mNote = new NoteItem();
			mNote.Title = title;
			mNote.Content = content;			
			ContentMgr.getInstance(this).insertNote(mNote);
		}
		else
		{
			mNote.Title = title;
			mNote.Content = content;			
			ContentMgr.getInstance(this).updateNote(mNote);
		}
	}
}
